package com.dh.leetcode.eight;

import org.junit.Assert;
import org.junit.Test;

/**
 * @ClassName: _852_peak_index_in_a_mountain_array
 * @Description: 符合下列属性的数组 arr 称为 山脉数组 ：
 * arr.length >= 3
 * 存在 i（0 < i < arr.length - 1）使得：
 * arr[0] < arr[1] < ... arr[i-1] < arr[i]
 * arr[i] > arr[i+1] > ... > arr[arr.length - 1]
 * 给你由整数组成的山脉数组 arr ，返回任何满足 arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 的下标 i 。
 * <p>
 *  
 * <p>
 * 示例 1：
 * <p>
 * 输入：arr = [0,1,0]
 * 输出：1
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/peak-index-in-a-mountain-array
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @Author: shouzimu
 * @Date: 2021/6/15 9:34
 */
public class _852_peak_index_in_a_mountain_array {

    public int peakIndexInMountainArray(int[] arr) {
        int len = arr.length;
        if (len == 0) {
            return -1;
        }
        int index = 0;
        int max = arr[0];
        for (int i = 1; i < len; i++) {
            if (arr[i] > max) {
                max = arr[i];
                index = i;
            } else {
                break;
            }
        }
        return index;
    }

    @Test
    public void peakIndexInMountainArrayTest() {
        Assert.assertEquals(1, peakIndexInMountainArray(new int[]{0, 1, 0}));
        Assert.assertEquals(1, peakIndexInMountainArray(new int[]{0, 2, 1, 0}));
        Assert.assertEquals(1, peakIndexInMountainArray(new int[]{0, 10, 5, 2}));
        Assert.assertEquals(2, peakIndexInMountainArray(new int[]{3, 4, 5, 1}));
        Assert.assertEquals(2, peakIndexInMountainArray(new int[]{24, 69, 100, 99, 79, 78, 67, 36, 26, 19}));
    }
}
